/***************************************************************************************
Firm-embedded productivity and cross-country income differences
Alviarez, Cravino and Ramondo
Journal of Political Economy (2022)

Program: figure_04_phi_vs_manuf_serv.do
Date: October 2022

Description: Reproduces Figure 2: Dev. accounting: firm-embedded productivity vs country-embedded factors.

*****************************************************************************************/

*-------------------------------------------------------------------------------
global typeden=1
include "set_directories.do"
set memory 64g
global lf "LF"

*Log
cap log close
log using "${clogs}/figure_04_phi_vs_manuf_serv.log",replace
*-------------------------------------------------------------------------------



*-----------------------------------------------------------------
clear all
use "${data}/estimates_aggtype_naics_sales_s1_base_woparent.dta", clear

local grvar0 "LP_ppp_emp_pwt" 
local grvar1 ""	
local rr ""
local var3 "A"
local yy "2016"
local yy_ref=`yy'	
replace year=`yy_ref'
merge m:1 year isocode type using "${data}/aggregates_tfp_lp_klems_agg.dta" 
drop if _merge==2
drop _merge

foreach var in D_A  {
gen `var'_phi=(-1)*`var'*(0.2)
*---------------------------------------
gen b_`var'_phi_`grvar0'=ln_`grvar0' - `var'_phi
}

local LHS1 D_`var3'_phi
local LHS2 b_D_`var3'_phi_`grvar0'
local RHS ln_`grvar0'
	
sum `LHS1' `RHS'  `LHS2'
local ytitle "{&Delta}`=ustrunescape("\u03D5\u0303")'{sub:n}, {&Delta}`=ustrunescape("\u007A\u0303")'{sub:n}"
local xtitle "{&Delta}y{sub:n}"
local firm_emb "{&Delta}`=ustrunescape("\u03D5\u0303")'{sub:n}" 
local ctry_emb "{&Delta}`=ustrunescape("\u007A\u0303")'{sub:n}"
local title "Affiliates older than `edad' years"
local title ""

tempfile temp
save `temp', replace


*Manufacturing 
*-------------------------------------------------------------------------------------
use `temp', clear
keep if type=="manuf"

reg `LHS1' `RHS'
mat b = e(b)
mat V = e(V)
global c1: display %-03.2fc round(b[1,1],0.01)
global se1: display %-03.2fc round(sqrt(V[1,1]),0.01)

reg `LHS2' `RHS' 
mat b = e(b)
mat V = e(V)
global c2: display %-03.2fc round(b[1,1],0.01)
global se2: display %-03.2fc round(sqrt(V[1,1]),0.01)

display $c1
display $se1
display $c2
display $se2


global opt1 msymbol(circle_hollow) mlabel(isocode) mlabcolor(red)  msize(small) mlabsize(small)
global opt2 msymbol(square_hollow) mlabel(isocode) mlabcolor(blue) msize(small) mlabsize(small)
two (scatter `LHS1' `RHS', sort $opt1  mcolor(red)) (lfit `LHS1' `RHS', lcolor(red)) (line `RHS' `RHS', lcolor(none))  ///
	(scatter `LHS2' `RHS', sort $opt2  mcolor(blue)) (lfit `LHS2' `RHS', lcolor(blue)) (line `RHS' `RHS', lcolor(none)),  ylabel(-1(0.5)0.5, format(%5.1f) labsize(medlarge)) xlabel(-1.2(0.4)0.4, format(%5.1f) labsize(medlarge)) ///
	title("`title'", size(small)) ytitle("`ytitle'", size(medlarge)) xtitle("`xtitle'", size(medlarge)) graphregion(fcolor(white) lcolor(white)) ///
	legend(order(2 "`firm_emb': $c1 ($se1)" 5 "`ctry_emb': $c2 ($se2)") rows(2) size(medlarge) bplace(se) ring(0) region(lwidth(none))) 

	
graph export "${rmain}/fig_04_manuf.pdf", replace


*Services 
*-------------------------------------------------------------------------------------
use `temp', clear
keep if type=="serv"

reg `LHS1' `RHS'
mat b = e(b)
mat V = e(V)
global c1: display %-03.2fc round(b[1,1],0.01)
global se1: display %-03.2fc round(sqrt(V[1,1]),0.01)

reg `LHS2' `RHS' 
mat b = e(b)
mat V = e(V)
global c2: display %-03.2fc round(b[1,1],0.01)
global se2: display %-03.2fc round(sqrt(V[1,1]),0.01)

display $c1
display $se1
display $c2
display $se2


global opt1 msymbol(circle_hollow) mlabel(isocode) mlabcolor(red)  msize(small) mlabsize(small)
global opt2 msymbol(square_hollow) mlabel(isocode) mlabcolor(blue) msize(small) mlabsize(small)
two (scatter `LHS1' `RHS', sort $opt1  mcolor(red)) (lfit `LHS1' `RHS', lcolor(red)) (line `RHS' `RHS', lcolor(none))  ///
	(scatter `LHS2' `RHS', sort $opt2  mcolor(blue)) (lfit `LHS2' `RHS', lcolor(blue)) (line `RHS' `RHS', lcolor(none)),  ylabel(-1(0.5)0.5, format(%5.1f) labsize(medlarge)) xlabel(-0.8(0.2)0.4, format(%5.1f) labsize(medlarge)) ///
	title("`title'", size(small)) ytitle("`ytitle'", size(medlarge)) xtitle("`xtitle'", size(medlarge)) graphregion(fcolor(white) lcolor(white)) ///
	legend(order(2 "`firm_emb': $c1 ($se1)" 5 "`ctry_emb': $c2 ($se2)") rows(2) size(medlarge) bplace(se) ring(0) region(lwidth(none))) 

	
graph export "${rmain}/fig_04_serv.pdf", replace
	